from flask import render_template,redirect,request
from . import api_0_2
import json

from .spider.login_spider import login_hlju
from .spider.userinfo_spider import request_userinfo
from .spider.courses_spider import request_courses
from .spider.scores_spider import request_scores
from .spider.credit_spider import request_credit
from .spider.exams_spider import request_exams
from .spider.result import result
from ..models import News

def login_hdbox(uid,pwd):
    result = login_hlju(uid,pwd)
    data = json.loads(result)
    if data.get('code') != 100:
        return result
    r =data.get('result')[0].get('token')
    print('login result:',r)
    return r


@api_0_2.route('/0.2/login',methods=['GET','POST'])
def login():
    if request.method=='GET':
        return result(102)
    try:
        uid = request.form['uid']
        pwd = request.form['pwd']
        login_result = login_hdbox(uid,pwd)
        if login_result:
            return request_userinfo(uid,login_result)
        else:
            return result(104)
    except KeyError as e:
        return result(103)

@api_0_2.route('/0.2/userinfo',methods=['GET','POST'])
def userinfo():
    if request.method=='GET':
        return result(102)
    try:
        uid = request.form['uid']
        pwd = request.form['pwd']
        login_result = login_hdbox(uid,pwd)
        if login_result:
            return request_userinfo(uid,login_result)
        else:
            return result(104)
    except KeyError as e:
        return result(103)


@api_0_2.route('/0.2/credit',methods=['GET','POST'])
def credit():
    if request.method=='GET':
        return result(102)
    try:
        uid = request.form['uid']
        pwd = request.form['pwd']
        login_result = login_hdbox(uid,pwd)
        if login_result:
            return request_credit(uid,login_result)
        else:
            return result(104)
    except KeyError as e:
        return result(103)


@api_0_2.route('/0.2/courses',methods=['GET','POST'])
def courses():
    if request.method=='GET':
        return result(102)
    try:
        uid = request.form['uid']
        pwd = request.form['pwd']
        login_result = login_hdbox(uid,pwd)
        if login_result:
            return request_courses(uid,login_result)
        else:
            return result(104)
    except KeyError as e:
        return result(103)


@api_0_2.route('/0.2/scores',methods=['GET','POST'])
def scores():
    if request.method=='GET':
        return result(102)
    try:
        uid = request.form['uid']
        pwd = request.form['pwd']
        login_result = login_hdbox()
        if login_result:
            return request_scores(uid,login_result)
        else:
            return result(104)
    except KeyError as e:
        return result(103)


@api_0_2.route('/0.2/exams',methods=['GET','POST'])
def exams():
    if request.method=='GET':
        return result(102)
    try:
        uid = request.form['uid']
        pwd = request.form['pwd']
        login_result = login_hdbox(uid,pwd)
        if login_result:
            return request_exams(uid,login_result)
        else:
            return result(104)
    except KeyError as e:
        return result(103)

@api_0_2.route('/0.2/newslist',methods=['GET','POST'])
def newslist():
    if request.method=='GET':
        page = request.args.get('page',1,type=int)
    else:
        page = int(request.form['page'])
    # 按照发布时间降序排序
    pagination = News.query.order_by(-News.pubTime).paginate(page,per_page=3,error_out=False)

    newslist = pagination.items

    return result(100,[news.to_json_list() for news in newslist])


@api_0_2.route('/0.2/newsdetail',methods=['GET','POST'])
def newsdetail():
    if request.method=='GET':
        nid = request.args.get('nid',0,type=int)
    else:
        nid = int(request.form['nid'])

    if nid == 0:
        return result(103)

    news = News.query.filter_by(nid=nid).first()

    return result(100,[news.to_json_detail()])
